package com.adcc.pushXml.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.charset.Charset;

/**
 * @author LHB
 * @date 2019/12/3 8:53
 * UDP
 */
public class Udp {
    private Logger log = LoggerFactory.getLogger(Udp.class);
    private String host;
    private Integer port;
    private DatagramPacket datagramPacket;
    private Integer recvDataLength = 1500;
    private volatile DatagramSocket serverSocket = null;

    public Udp() {
    }

    public Udp(String host, Integer port) {
        this.host = host;
        this.port = port;

    }

    /**
     * 初始化socket
     */
    public void init() {

        if (serverSocket == null) {
            synchronized (this) {
                if (serverSocket == null) {
                    try {
                        serverSocket = new DatagramSocket();
                    } catch (
                            SocketException e) {
                        log.error("socket init error", e);
                    }
                }
            }

        }


    }

    /**
     * 发送数据
     *
     * @return
     */
    public void sendMsg(String msg) throws IOException {
        log.info("host:{} ######## port:{},####### msg: {}", host, port, msg);
        if (StringUtils.isEmpty(host) || port <= 0 || StringUtils.isEmpty(msg)) {
            return;
        }
        byte[] data = msg.getBytes("UTF-8");
        datagramPacket = new DatagramPacket(data, data.length, new InetSocketAddress(host, port));

        serverSocket.send(datagramPacket);
    }

    /**
     * 接受数据
     */
    public String receiveData() throws IOException {
        byte[] buff = new byte[recvDataLength];
        DatagramPacket dp = new DatagramPacket(buff, recvDataLength);
        this.serverSocket.receive(dp);
        host = dp.getAddress().getHostAddress();
        String line = new String(dp.getData(), 0, dp.getLength(), "utf-8");
        log.info("recv UDP host:{} msg:{}", host, line);
        return line;
    }


    /**
     * 关闭链接
     */
    public void close() {
        if (this.serverSocket != null) {
            this.serverSocket.close();
            this.serverSocket = null;
        }
    }


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public DatagramPacket getDatagramPacket() {
        return datagramPacket;
    }

    public void setDatagramPacket(DatagramPacket datagramPacket) {
        this.datagramPacket = datagramPacket;
    }

    public DatagramSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(DatagramSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public Integer getRecvDataLength() {
        return recvDataLength;
    }

    public void setRecvDataLength(Integer recvDataLength) {
        this.recvDataLength = recvDataLength;
    }
}
